/*
 * Copyright 2011 Angel Sanadinov
 *
 * This file is part of VBox WMI.
 *
 * VBox WMI is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VBox WMI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
 */

package Beans;

import Utilities.Constants;
import Utilities.Constants.VirtualResourceType;
import Utilities.Functions;
import java.sql.Timestamp;
import java.io.Serializable;
import Utilities.Interfaces.SelfValidating;

/**
 * This class is used for storing virtual resource data when writing it to or
 * retrieving it from the database.
 *
 * @see VirtualMachineBean
 * @see VirtualMediumBean
 *
 * @author Angel Sanadinov
 */
public class VirtualResourceBean implements Serializable, SelfValidating
{
    private String resourceId;       //the UUID of the resource
    private int serverId;            //the id of the server on which the resource resides
    private int ownerId;             //the id of the owner of the resource
    private Timestamp dateCreated;   //the date on which the resource was created *
    private String permissions;      //the permissions associated with the resource
    private VirtualResourceType resourceType;//the type of the resource
    //* only available when object is built with data from the database

    /**
     * Constructs a new object, most often user when resource data is to be put in the database.
     *
     * @param resourceId the UUID of the resource
     * @param serverId the id of the server on which the resource resides
     * @param ownerId the id of the resource owner
     * @param permissions the permissions associated with the resource
     * @param resourceType the type of the resource
     */
    public VirtualResourceBean(String resourceId, int serverId, int ownerId, String permissions, VirtualResourceType resourceType)
    {
        this.resourceId = resourceId;
        this.serverId = serverId;
        this.ownerId  = ownerId;
        this.permissions = permissions;
        this.resourceType = resourceType;
    }

    /**
     * Constructs a new object, most often user when resource data is to be retrieved
     * from the database.
     *
     * @param resourceId the UUID of the resource
     * @param serverId the id of the server on which the resource resides
     * @param ownerId the id of the resource owner
     * @param dateCreated the date on which the resource was created
     * @param permissions the permissions associated with the resource
     * @param resourceType the type of the resource
     */
    public VirtualResourceBean(String resourceId, int serverId, int ownerId, Timestamp dateCreated, String permissions, int resourceType)
    {
        this(resourceId, serverId, ownerId, permissions, VirtualResourceType.INVALID);
        this.dateCreated = dateCreated;

        switch(resourceType)
        {
            case Constants.DB_RESOURCE_VIRTUAL_MACHINE: this.resourceType = VirtualResourceType.MACHINE; break;
            case Constants.DB_RESOURCE_VIRTUAL_MEDIUM: this.resourceType = VirtualResourceType.MEDIUM; break;
            case Constants.DB_RESOURCE_VIRTUAL_NETWORK: this.resourceType = VirtualResourceType.NETWORK; break;
            default: this.resourceType = VirtualResourceType.INVALID;
        }
    }

    /**
     * Checks the validity of the object. <br><br>
     *
     * This object is valid only when it is built with data retrieved from the
     * database.
     *
     * @return <code>true</code> if the object is valid or <code>false</code> otherwise
     */
    @Override
    public boolean isValid()
    {
        if(Functions.isUUIDValid(resourceId) && Functions.isServerIdValid(serverId)
                && Functions.isUserIdValid(ownerId) && Functions.isTimestampValid(dateCreated)
                && Functions.arePermissionsValid(permissions) 
                && resourceType != null && resourceType != VirtualResourceType.INVALID)
            return true;
        else
            return false;
    }

    /**
     * Retrieves the UUID associated with this resource.
     *
     * @return the UUID of the resource
     */
    public String getResourceId()
    {
        return resourceId;
    }

    /**
     * Retrieves the id of the server on which this resource resides.
     *
     * @return the server id
     */
    public int getServerId()
    {
        return serverId;
    }

    /**
     * Retrieves the id of the resource's owner.
     *
     * @return the owner id
     */
    public int getOwnerId()
    {
        return ownerId;
    }

    /**
     * Retrieves the date on which the resource was added in the database.
     *
     * @return the resource creation date
     */
    public Timestamp getDateCreated()
    {
        return (Timestamp)dateCreated.clone();
    }

    /**
     * Retrieves the permissions associated with the resource.
     * 
     * @return the resource's permissions
     */
    public String getPermissions()
    {
        return permissions;
    }

    /**
     * Retrieves the type of the virtual resource.
     *
     * @return the type of the virtual resource
     */
    public VirtualResourceType getResourceType()
    {
        return resourceType;
    }

    /**
     * Builds a CSV list of the data stored in the object. <br><br>
     *
     * Format: <i>resourceId,serverId,ownerId,dateCreated,permissions</i>
     *
     * @return the <code>String</code> representation of the object
     */
    @Override
    public String toString()
    {
        return resourceId + "," + serverId + "," + ownerId + "," + dateCreated + "," + permissions;
    }
}
